package uit.qlks.from;

import java.awt.event.KeyEvent;
import java.math.BigInteger;
import java.security.MessageDigest;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import uit.ejb.dto.UserDTO;
import uit.qlks.controller.user.UserController;

/**
 * 
 * @author SVN - Team
 */
public class frmLogin extends javax.swing.JFrame {

    /**
     * Creates new form Login
     */
    public static UserDTO _us = new UserDTO();
    public static frmLogin _frm;

    public frmLogin() {
        initComponents();
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.setResizable(false);
        txtTenDangNhap.requestFocusInWindow();
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtTenDangNhap = new javax.swing.JTextField();
        txtMatKhau = new javax.swing.JPasswordField();
        btnDangNhap = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Đăng Nhập");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/matkhau.png"))); // NOI18N
        jLabel1.setText("ĐĂNG NHẬP");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel2.setText("Tên đăng nhập:");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel3.setText("Mật khẩu:");

        txtTenDangNhap.setFont(new java.awt.Font("Times New Roman", 0, 16));
        txtTenDangNhap.setName("txtTenDangNhap"); // NOI18N
        txtTenDangNhap.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTenDangNhapKeyPressed(evt);
            }
        });

        txtMatKhau.setFont(new java.awt.Font("Times New Roman", 0, 16));
        txtMatKhau.setName("txtMatKhau"); // NOI18N
        txtMatKhau.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTenDangNhapKeyPressed(evt);
            }
        });

        btnDangNhap.setFont(new java.awt.Font("Times New Roman", 0, 16));
        btnDangNhap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/dangnhap.png"))); // NOI18N
        btnDangNhap.setText("Đăng nhập");
        btnDangNhap.setBorder(null);
        btnDangNhap.setContentAreaFilled(false);
        btnDangNhap.setName("btnDangNhap"); // NOI18N
        btnDangNhap.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/dangnhap2.png"))); // NOI18N
        btnDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDangNhapActionPerformed(evt);
            }
        });

        btnThoat.setFont(new java.awt.Font("Times New Roman", 0, 16));
        btnThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Exit1.png"))); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.setBorder(null);
        btnThoat.setContentAreaFilled(false);
        btnThoat.setMaximumSize(new java.awt.Dimension(85, 23));
        btnThoat.setMinimumSize(new java.awt.Dimension(85, 23));
        btnThoat.setName("btnThoat"); // NOI18N
        btnThoat.setPreferredSize(new java.awt.Dimension(85, 23));
        btnThoat.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Exit2.png"))); // NOI18N
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtTenDangNhap)
                            .addComponent(txtMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(btnDangNhap)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        txtTenDangNhap.getAccessibleContext().setAccessibleName("txtTenDangNhap");
        txtMatKhau.getAccessibleContext().setAccessibleName("txtMatKhau");
        btnDangNhap.getAccessibleContext().setAccessibleName("btnDangNhap");
        btnThoat.getAccessibleContext().setAccessibleName("btnHuy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-343)/2, (screenSize.height-273)/2, 343, 273);
    }// </editor-fold>//GEN-END:initComponents

private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
// TODO add your handling code here:
    int result = JOptionPane.showConfirmDialog(this, "Bạn có chắc chắn muốn thoát không?", "Thoát chương trình", 0, 3);
    if (result == JOptionPane.YES_OPTION) {
        frmMain._main.setVisible(true);
        frmMain._main.setFocusable(true);
        this.dispose();
    }
}//GEN-LAST:event_btnThoatActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    // TODO add your handling code here:
}//GEN-LAST:event_formWindowClosed
    public static String encryptMD5(String code) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(code.getBytes());
            BigInteger dis = new BigInteger(1, md5.digest());
            code = dis.toString(16);
            return code;
        } catch (Exception e) {

            return code;
        }
    }

private void btnDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDangNhapActionPerformed
// TODO add your handling code here:
    dangnhap();
}//GEN-LAST:event_btnDangNhapActionPerformed
    private void dangnhap() {
        try {
            if ("".equals(txtTenDangNhap.getText())) {
                JOptionPane.showMessageDialog(rootPane, "Chưa nhập tên đăng nhập.", "Warning", JOptionPane.WARNING_MESSAGE);
                txtTenDangNhap.requestFocusInWindow();
                return;
            }
            if ("".equals(txtMatKhau.getText())) {
                JOptionPane.showMessageDialog(rootPane, "Chưa nhập mật khẩu.", "Warning", JOptionPane.WARNING_MESSAGE);
                txtMatKhau.requestFocusInWindow();
                return;
            }
            _us.setUserName(txtTenDangNhap.getText());
            _us.setPassword(encryptMD5(txtMatKhau.getText()));
            int temp = UserController.user_CheckLogin(_us.getUserName(), _us.getPassword());
            _us.setRole(Integer.toString(temp));

            if (Integer.parseInt(_us.getRole()) >= 0) {
                frmMain._main.setVisible(true);
                frmMain._main.setFocusable(true);
                _frm = this;
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Mật khẩu hoặc tên đăng nhập không chính xác.", "Thông báo", JOptionPane.ERROR_MESSAGE);
                txtTenDangNhap.requestFocusInWindow();
                txtMatKhau.setText("");
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
    }
private void txtTenDangNhapKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTenDangNhapKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        dangnhap();
    }
}//GEN-LAST:event_txtTenDangNhapKeyPressed

    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDangNhap;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField txtMatKhau;
    private javax.swing.JTextField txtTenDangNhap;
    // End of variables declaration//GEN-END:variables
}
